package com.lyw.home.vm

import android.content.Context
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.blankj.utilcode.util.LogUtils
import com.lu.library.CommLibInit
import com.lu.library.base.BaseViewModel
import com.lyw.db.DatabaseProvider
import com.lyw.home.Poety
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

data class PoetryUiState(var author:String,var avatar:String?,var content:String)


class PoetryDetailVm:ViewModel() {

//    private val _poetry = MutableLiveData<PoetryUiState>()
//    val poetry: LiveData<PoetryUiState> = _poetry

    private val _uiStateFlow = MutableStateFlow(Poety("",null,"","",0))
    val uiStateFlow: StateFlow<Poety> = _uiStateFlow.asStateFlow()

    fun init(id:Int){
        LogUtils.d("id:$id")
        viewModelScope.launch {


        }

    }

    /**
     * 切换是否喜欢
     */
    fun toggleLike() {
        viewModelScope.launch {
            val data = uiStateFlow.value
            var like = !data.like
            withContext(Dispatchers.IO){
            }
            _uiStateFlow.update {
                it.copy(like = like)
            }
        }
    }
}